﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLK.Database.BLLs;
using QLK.Database;
using QLK.Web.App_Code;
using Utilities;

namespace QLK.Web
{
    public partial class SearchProducts : System.Web.UI.Page
    {
        private string _ID;
        private ProductBLL ProBLL = new ProductBLL();
        private CategoryBLL CateBLL = new CategoryBLL();
        private ManufactureBLL MfBLL = new ManufactureBLL();
        private Invoices_Product invoiceproduct;
        private Invoices_ProductBLL InvoiceProBLL = new Invoices_ProductBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            _ID = Request.QueryString["ID"];
            if (!IsPostBack)
                Load_Data();
        }

        protected void grvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {            
            grvProducts.PageIndex = e.NewPageIndex;
            Load_Data();
        }

        private void Load_Data()
        {
            //load data for Categories and Manufaceture

            drlCateID.DataSource = CateBLL.Cate_GetAll();
            drlCateID.DataValueField = Columns.CategoryID;
            drlCateID.DataTextField = Columns.CategoryName;
            drlCateID.DataBind();

            drlManufacture.DataSource = MfBLL.ManufactureGetAll();
            drlManufacture.DataValueField = Columns.ManufactureID;
            drlManufacture.DataTextField = Columns.ManufactureName;
            drlManufacture.DataBind();

            grvProducts.DataSource = ProBLL.ProductGetForGrid();
            grvProducts.DataBind();

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_ID))
                lblWarning.Text = "There is nothing to save!";
            else
            {
                if (_ID == "0")
                {
                    CheckBox ck;
                    invoiceproduct = new Invoices_Product();
                    for (int i = 0; i < grvProducts.Rows.Count; i++)
                    {
                        ck = (CheckBox)grvProducts.Rows[i].Cells[0].FindControl("ckChoose");
                        if (ck.Checked == true)
                        {
                            invoiceproduct.InvoiceID = int.Parse(Request.QueryString[Columns.InvoiceID]);
                            invoiceproduct.ProductID = int.Parse(ck.Attributes["_ProID"].ToString());
                            invoiceproduct.Price = int.Parse(grvProducts.Rows[i].Cells[6].Text);
                            invoiceproduct.Type = true;

                            if (InvoiceProBLL.Invoices_ProductInsert(invoiceproduct))
                            {
                                
                            }
                        }
                    }

                    Response.Redirect(string.Format("{0}", PageUrls.ExportInvoiceProducts));
                }
                if (_ID == "1")
                { }

            }
        }

        protected void ImgProName_Click(object sender, ImageClickEventArgs e)
        {
            if (string.IsNullOrEmpty(txtProName.Text))
                lblWarning.Text = "**Type sometext into the textbox!!";
            else
            {
                if (ProBLL.ProductGetByProductName(txtProName.Text) == null)
                    lblWarning.Text = "**No result found!";
                else
                {
                    grvProducts.DataSource = ProBLL.ProductGetByProductName(txtProName.Text);
                    grvProducts.DataBind();
                }
            }
        }

        protected void ImgUPC_Click(object sender, ImageClickEventArgs e)
        {
            if (string.IsNullOrEmpty(txtUPC.Text))
                lblWarning.Text = "**Type sometext into the textbox!!";
            else
            {
                if (ProBLL.ProductGetByUPC(txtUPC.Text) == null)
                    lblWarning.Text = "**No result found!";
                else
                {
                    grvProducts.DataSource = ProBLL.ProductGetByUPC(txtUPC.Text);
                    grvProducts.DataBind();
                    lblWarning.Text = "";
                }
            }
        }

        protected void ImgCateID_Click(object sender, ImageClickEventArgs e)
        {
            int id = int.Parse(drlCateID.SelectedValue);

            if (ProBLL.ProductGetByCateID(id) == null)
                lblWarning.Text = "**No result found!";
            else
            {
                grvProducts.DataSource = ProBLL.ProductGetByCateID(id);
                grvProducts.DataBind();
            }
        }

        protected void ImgManufacture_Click(object sender, ImageClickEventArgs e)
        {
            int id = int.Parse(drlManufacture.SelectedValue);

            if (ProBLL.ProductGetByManuID(id) == null)
                lblWarning.Text = "**No result found!";
            else
            {
                grvProducts.DataSource = ProBLL.ProductGetByManuID(id);
                grvProducts.DataBind();
            }
        }

        protected void ImgPrice_Click(object sender, ImageClickEventArgs e)
        {
            if (ConvertHelper.ConvertToInt(txtPricefrom.Text) && ConvertHelper.ConvertToInt(txtPriceto.Text))
            {
                if (ProBLL.ProductGetByPrices(int.Parse(txtPricefrom.Text),
                        int.Parse(txtPriceto.Text)) == null)
                    lblWarning.Text = "**No result found!";
                else
                {
                    grvProducts.DataSource = ProBLL.ProductGetByPrices(int.Parse(txtPricefrom.Text),
                        int.Parse(txtPriceto.Text));
                    grvProducts.DataBind();
                }
            }
            else
            {
                lblWarning.Text = "**Enter number into Prices!!";
            }
        }

       

       
    }
}
